import React from 'react'
import { View, Text, ScrollView } from 'react-native'
import CustomImage from '@components/image/CustomImage'
import LinearGradient from 'react-native-linear-gradient'
import CustomButton from '@components/button/CustomButton'
import CustomFlatList from 'src/components/custom-flat-list'
import { styles, empty } from '../css/index'
import moment from 'moment'
import { CommonToast } from 'src/utils/ToastUtil'
import RefreshState from 'src/components/custom-flat-list/RefreshState'
import CustomModalAnimatable from '@/components/modal/CustomModalAnimatable'
import PropTypes from 'prop-types'
import CustomBoxShadow from 'src/components/boxshadow/CustomBoxShadow'
import {
  setTop, // 删除岗位
  memberPay // 获取支付凭证
} from '@utils/RequestAPI'
import { WXPay, IAPPay } from 'src/utils/PayUtil' // 支付
import { IS_IOS } from 'src/utils/ScreenUtil'
import { isString } from 'src/utils/JSTypeUtil'
const stateType = ['招聘中', '未上架', '未付款', '平台下架', '平台永久下架', '上架待审核', '审核退回']
const ColorList = [{ color: '#64D59C' }, { color: '#999999' }, {}, { color: '#EF5E6A' }, { color: '#999999' }, { color: '#64D59C' }, { color: '#EF5E6A' }]

class JobPartTimeBody extends React.Component {
  static propTypes = {
    jumpVip: PropTypes.func,
    jumpFn: PropTypes.func,
    getMyPostList: PropTypes.func,
    type: PropTypes.number,
    data: PropTypes.array
  }

  constructor (props) {
    super(props)
    this.state = {
      upgradeMemberModal: false,
      item: {}
    }
  }

  toggleMask = (modal) => {
    this.setState({
      ...modal
    })
  }

  upgradeMember = () => {
    this.toggleMask({ upgradeMemberModal: false })
    this.props.jumpVip()
  }

  shutModal = () => {
    this.setState({
      upgradeMemberModal: false
    })
  }

  einmal = () => {
    const { haveCompanyApply, userinfo } = this.props
    const params = {
      businessType: 'refreshPost',
      postId: this.state.item.id,
      userId: userinfo.B.userId,
      companyId: haveCompanyApply.companyId,
      orderName: '岗位单次刷新'
    }
    if (IS_IOS) {
      params['payWay'] = 'iosApp'
      memberPay({ params })
        .then(res => {
          const { iapId, recordId } = res.data
          const { companyId, userId, postId } = params
          return IAPPay({ iapId, recordId, postId, companyId, userId, businessType: 'REFRESH_POST' })
        }).then((res) => {
          this.shutModal(false)
          CommonToast('刷新成功')
          this.props.getMyPostList(this.props.type, 0, 990)
        }).catch(error => {
          isString(error) && CommonToast(error)
        })
    } else {
      params['payWay'] = 'wxApp'
      memberPay({ params }).then((res) => {
        return WXPay(res.data)
      }).then((res) => {
        CommonToast('刷新成功')
        this.shutModal(false)
        this.props.getMyPostList(this.props.type, 0, 990)
      }).catch(error => {
        isString(error) && CommonToast(error)
      })
    }
    // 单次刷新 调用支付接口
  }

  render () {
    const data = this.props.data
    const { upgradeMemberModal } = this.state
    const { jumpFn, type } = this.props
    return <ScrollView contentContainerStyle={styles.jobPartTimeBody}>
      {
        (data || []).map((item, index) => {
          return <CustomBoxShadow key={index} style={styles.customBoxShadow}>
            <CustomButton key={index} style={styles.cell} onPress={this.cellClick} params={{ item }}>
              <View style={styles.top}>
                <View style={styles.topLeft}>
                  <Text style={styles.name}>{item.name}</Text>
                  {item.type === 1 && item.practiceLabel !== 1 && <CustomImage style={styles.tagImg} source={require('@/imgC/home/tag_quanzhi.png')} />}
                  {item.type === 0 && <CustomImage style={styles.tagImg} source={require('@/imgC/home/tag_jianzhi.png')} />}
                  {item.type === 1 && item.practiceLabel === 1 && <CustomImage style={styles.tagImg} source={require('@/imgC/home/tag_shixi.png')} />}
                  {item.isFast === 1 && <CustomImage style={styles.tagImg} source={require('@/imgC/home/tag_fast.png')} />}
                </View>
                {/* <View style={styles.topRight}> */}
                <View>
                  <Text style={styles.dataitem}>{this.momentFn(item.gmtCreate)}</Text>
                </View>
              </View>
              {/* </View> */}
              <View style={styles.viewPrice}>
                <Text style={styles.unit}>
                  {`浏览${item.viewCount}`}
                </Text>
                <Text style={styles.unit}>
                  {`分享${item.entryCount}`}
                </Text>
                <Text style={styles.unit}>
                  {`收藏${item.collectionCount}`}
                </Text>
                <Text style={styles.unit}>
                  {`沟通${item.resumeCount}`}
                </Text>
              </View>
              <View style={styles.bottom}>
                <View style={styles.bottomLeft}>
                  <View style={styles.tagView} ><Text style={[styles.tagText, ColorList[item.state]]}>{stateType[item.state]}</Text></View>
                  {item.state == 0 && <CustomButton onPress={this.refres} params={{ item }} style={styles.refreshView}><Text style={styles.refreshText}>刷新</Text></CustomButton>}
                </View>
                {/* todo 面试状态 */}
              </View>
            </CustomButton>
          </CustomBoxShadow>
        })
      }
      {
        (data || []).length === 0 && <View style={empty.view}>
          <CustomImage style={empty.viewImg} source={require('@/imgC/company/administrators_empty.png')} />
          <Text style={empty.viewTextone}>无职位管理</Text>
          <Text style={empty.viewTextTow}>快去发布职位吧～</Text>
        </View>
      }
      <CustomModalAnimatable onCancel={this.shutModal} visible={upgradeMemberModal}>
        <View style={styles.updateStateModalView}>
          <Text style={styles.updateStateModalTitle}>提醒</Text>
          <Text style={styles.updateStateModalText}>您的权限不足是否升级为会员</Text>
          <CustomButton onPress={this.einmal} style={styles.updateStateModalNoBtn}><Text style={styles.updateStateModalNoBtnText}>单次刷新</Text></CustomButton>
          <CustomButton onPress={this.upgradeMember} style={styles.updateStateModalYesBtn}><Text style={styles.updateStateModalYesBtnText}>升级会员</Text></CustomButton>
        </View>
      </CustomModalAnimatable>
    </ScrollView >
  }

  momentFn = (date) => {
    // 86399000
    const min = moment().format('YYYY-MM-DD') + ' 00:00:00'
    const max = moment().format('YYYY-MM-DD') + ' 23:59:59'
    if (moment(min) < moment(date) && moment(date) < moment(max)) {
      return '今天' + moment(date).format('HH:MM')
    } else if (moment(min) - 86399000 < moment(date) && moment(date) < moment(max) - 86399000) {
      return '昨天' + moment(date).format('HH:MM')
    } else {
      return moment(date).format('MM-DD HH:MM')
    }
  }

  cellClick = ({ item }) => {
    this.props.jumpFn(item)
  }

  refres = ({ item }) => {
    setTop({ params: { postId: item.id } }).then((data) => {
      if (data.code == 102) {
        this.setState({
          item: item
        })
        this.toggleMask({ upgradeMemberModal: true })
      } else {
        if (data.data) {
          CommonToast('刷新成功')
          this.props.getMyPostList(this.props.type, 0, 990)
        } else {
          CommonToast('刷新失败')
        }
      }
    })
  }
}
export default JobPartTimeBody
